import 'dart:math';
import 'dart:ui';
import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/error_widget.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/ui/widget/search_bar.dart';
import 'package:s_mankey/ui/widget/show_toast.dart';
import 'package:s_mankey/view_model/enterprise_list_vmodel.dart';

class EnterprisePage extends StatefulWidget {
  final arguments;

  EnterprisePage({
    this.arguments,
  });

  @override
  _EnterprisePage createState() => _EnterprisePage(arguments: arguments);
}

class _EnterprisePage extends State<EnterprisePage> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  final arguments;

  _EnterprisePage({
    this.arguments,
  });

  TabController _controller;
  List caseDatas = [];
  List companyDatas = [];
  List areaDatas = [];
  int tabCurIndex = 0;
  int tabLength = 0;


//  var datas = [
//    '全部',
//    '精选',
//    '科技研发',
//    '科技研发',
//    '科技研发',
//    '科技研发',
//    '科技研发',
//    '科技研发',
//    '科技研发'
//  ];

  @override
  void initState() {
    _controller = TabController(initialIndex: tabCurIndex, length: tabLength, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;


  @override
  Widget build(BuildContext context) {
    return ProviderWidget<EnterpriseListVModel>(model: EnterpriseListVModel(),onModelReady: (model) => model.initData(),
    builder: (context, model ,child){
    return (model.datas == null)? LoadingWidget() :
      DefaultTabController(
      length: model.theCase.length,
      initialIndex: 0,
      child: Builder(
        builder: (context) {
        return Scaffold(
          backgroundColor: Colors.white,
            appBar: AppBar(
              automaticallyImplyLeading:false,
              elevation: 1,
              title: EnterpriseSearchBar(),
              bottom: TabBar(
                  indicatorColor: Color(0xffff3f7f,),
                  indicatorPadding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  labelPadding: EdgeInsets.fromLTRB(10,0,10,0),
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicatorWeight: 2,
                  labelStyle: TextStyle(fontSize: AppFontSize.Size16, color: Colors.black),
                  unselectedLabelStyle: TextStyle(fontSize: AppFontSize.Size14, color: Color(0xffbababa)),
                  isScrollable: true,
                  tabs: List.generate(
                      model.theCase.length,
                          (index) {
                            tabCurIndex = index;
                        return Tab(
                          text: model.theCase[index]['name'],
                        );
                          })),

            ),

            body: TabBarView(children: List.generate(model.theCase.length, (index) => EnterpriseList(datas: model.company[index],),)),
          );
        },
      ),
    );});
  }


}

/// 企业列表UI
class EnterpriseList extends StatefulWidget {

  final datas;

  EnterpriseList({this.datas});

  @override
  _EnterpriseList createState() => _EnterpriseList();

}

class _EnterpriseList extends State<EnterpriseList> {

  int listIndex = 0;

  @override
  Widget build(BuildContext context) {
//    print('widget.datas${widget.datas}');
//    print('widget.datas${widget.datas.length}');

    double imgW =   (AppCustomSize.ScreentWidght- 20);
    double singleImaMaigin = 5.0;
    double singleImgW = (AppCustomSize.ScreentWidght- 20) / 3 - singleImaMaigin ;

    return
      ListView.builder(
      itemCount: widget.datas.length,
      itemBuilder: (BuildContext context, int index) {
        listIndex = index;
        int imaLength = (widget.datas[index]['image'].length > 2)? 3 : widget.datas[listIndex]['image'].length;
        print('index$index');
        return GestureDetector(onTap: (){
//          Navigator.pushNamed(context, RouterName.enterpriseDetail);
        },child: Padding(
          padding: const EdgeInsets.all(10),
          child:

          Column(
          children: [
            /// 公司名称
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ClipOval(
                  child: Image.network(
                    widget.datas[index]['company_logo'],
                    height: 35,
                    width: 35,
                    fit: BoxFit.cover,
                  ),
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          widget.datas[index]['company_title'],
                          style: TextStyle(
                              color: Color(0xff2b2b2b),
                              fontSize: AppFontSize.Size14),
                        ),
                        Text(
                          widget.datas[index]['area_name'],
                          style: TextStyle(
                              color: Color(0xff939393),
                              fontSize: AppFontSize.Size12),
                        ),
                      ],
                    ),
                  ),
                ),

                ///TODO： 精选未完成
//                  Offstage(offstage: widget.datas[index]['is_choiceness'] != 0,child: Container(
//                    decoration: BoxDecoration(
//                        gradient: AppColor.gradientColor,
//                        borderRadius: BorderRadius.circular(10)),
//                    child: Container(
//                        margin: EdgeInsets.fromLTRB(8, 4, 8, 4),
//                        child:
//                        Text(
//                          '精选',
//                          style: TextStyle(
//                              fontSize: AppFontSize.Size10,
//                              color: Colors.white),
//                        )),
//                  ),),

              ],
            ),
//      中
            ///内容
            Container(
              padding: EdgeInsets.fromLTRB(0,10,0,10),
              alignment: Alignment(-1, 0),
              child: Text(
                widget.datas[index]['company_brief'].trim(),
                style: TextStyle(

                    fontSize: AppFontSize.Size14, color: Color(0xff71777e)),
              ),
            ),

            ///图片
            Visibility(visible:  (widget.datas[index]['image'].length != 0),child:  Container(
              width: imgW ,height: singleImgW,
              child:
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children:
                List.generate(widget.datas[index]['image'].take(3).toList().length ,
                      (imgIndex) =>
                    Container(
                      margin: EdgeInsets.fromLTRB((imgIndex == 1)? singleImaMaigin : 0, 0, (imgIndex == 1)? singleImaMaigin : 0, 0),
                      width: singleImgW,height: singleImgW, child: Image.network(widget.datas[index]['image'][imgIndex], fit: BoxFit.cover,),),)
              ),
            ),),


            ///TODO: 这里的圆角还没有实现
            ///地区
            Align(alignment: Alignment(-1, 0),child: IntrinsicWidth(child: Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
              padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
              decoration: BoxDecoration(
                  color: Color.fromRGBO(245, 247, 248, 0.7),
                  borderRadius: BorderRadius.circular(3)),
              child: Row(
                children: [
                  Container(
                    padding: EdgeInsets.fromLTRB(5, 1.3, 5, 1.3),
                    decoration: BoxDecoration(
//                      color: Color.fromRGBO(145, 247, 248, 0.7),
                    ),
                    child: Text(
                      '•',
                      style: TextStyle(
                          fontSize: AppFontSize.Size14,
                          color: Color(0xff0081ff)),
                    ),
                  ),
                  Container(
                    decoration: BoxDecoration(
//                      color: Color.fromRGBO(145, 247, 248, 0.7),
                    ),
                    child: Text(
                      '经营范围:',
                      style: TextStyle(
                          fontSize: AppFontSize.Size14,
                          color: Color(0xff0081ff)),
                    ),
                  ),
                  Expanded(child: Container(
                    padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                    decoration: BoxDecoration(
//                      color: Color.fromRGBO(145, 247, 248, 0.7),
                    ),
                    child: Text(
                      widget.datas[index]['company_business_scope'],
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: AppFontSize.Size14,
                        color: Color(0xffa4afbc),
                      ),
                    ),
                  ),),
                ],
              ),
            ),),),

            ///TODO: 这里的分割线受父视图限制, 找到超越父视图的方法再回来改
            Container(height: 1,width: window.physicalSize.width, decoration: BoxDecoration(color: AppColor.dividerColor),child: Text(''), ),
          ],
        ),

        ));
      },
    );
  }
}


///搜索控件widget
class EnterpriseSearchBar extends StatefulWidget {

  Function callBack; //点击搜索回调

  EnterpriseSearchBar({this.callBack});

  @override
  State<StatefulWidget> createState() => new _EnterpriseSearchBar();
}

class _EnterpriseSearchBar extends State<EnterpriseSearchBar> {

  final controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    return
      Row(
        children: [
          Expanded(child:

          GestureDetector(onTap: (){
            Navigator.pushNamed(context, RouterName.searchEnteprise);
//            Future.delayed(Duration(milliseconds: 500), (){
//              Navigator.pushNamed(context, RouterName.loginPage);
//            });

          },child: Container(
            margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
            height: 35,
            decoration: BoxDecoration(
                color: Color(0xfff4f5f7),
                borderRadius: BorderRadius.circular(30)),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(left: 5),
                  child: Icon(
                    Icons.search,
                    color: Colors.grey,
                    size: 20,
                  ),
                ),

                Expanded(child: TextField(
                  controller: controller,
                  decoration: new InputDecoration(
                      enabled: false,
                      contentPadding: EdgeInsets.fromLTRB(10, 0, 10, 11),
//                      hintText: '请输入手机号查询',
                      border: InputBorder.none),
                  // onChanged: onSearchTextChanged,
                ),),




              ],
            ),
          ),),),


//          Padding(
//            padding: const EdgeInsets.only(right: 10),
//            child: GestureDetector(
//              onTap: () {
//                Toast.toast(context,msg: "功能暂未开放 ");
//              },
//              child: Row(children: [
//                Icon(Icons.local_pizza,size: AppFontSize.Size18,),
//                Text('地区',style: TextStyle(fontSize: AppFontSize.Size16),),
//              ],),
//            ),
//          ),
        ],
      );

  }
}


